<template>
  <div class="content">
    <div class="common-seach-wrap">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="产品分类">
          <el-input v-model="search.categoryName" placeholder="请输入产品分类" clearable style="width: 200px" />
        </el-form-item>
        <el-form-item label="产品名称">
          <el-input v-model="search.name" placeholder="请输入产品名称" clearable style="width: 200px" />
        </el-form-item>
        <el-form-item label="分类索引">
          <el-select v-model="search.categoryIndex" placeholder="请选择分类索引" clearable style="width: 200px">
            <el-option label="窗膜" :value="0"></el-option>
            <el-option label="车衣" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getTableInfo">查询</el-button>
          <el-button type="" @click="resetSearch">重置</el-button>
          <el-button type="success" @click="handleOpen('add')">添加产品</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData" row-key="id" height="100%" stripe :border="true">
      <el-table-column prop="id" label="ID" fixed="left" width="50"></el-table-column>
      <el-table-column prop="categoryName" label="产品分类" fixed="left"></el-table-column>
      <el-table-column prop="categoryIndex" label="分类索引">
        <template #default="scope">
          <el-tag v-if="scope.row.categoryIndex == 0" type="success">窗膜</el-tag>
          <el-tag v-else type="warning">车衣</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="name" label="产品名称" fixed="left"></el-table-column>
      <el-table-column prop="" label="产品图片" width="150">
        <template #default="scope">
          <el-image style="width: 50px; height: 50px" :src="scope.row.image_url" :zoom-rate="1.2" :max-scale="7"
            :min-scale="0.2" :preview-src-list="[scope.row.image_url]" show-progress :initial-index="4" fit="cover"
            :preview-teleported="true" v-if="scope.row.image_url" />
          <el-text v-else>暂无图片</el-text>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="产品价格"></el-table-column>
      <el-table-column prop="sales" label="销量"></el-table-column>
      <el-table-column label="首页显示">
        <template #default="scope">
          <el-switch v-model="scope.row.show_in_home" @change="setShow(scope.row)" :active-value="1"
            :inactive-value="0"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" width="300"> </el-table-column>
      <el-table-column label="操作" fixed="right" width="300" align="center">
        <template #default="scope">
          <el-button type="primary" size="small" @click="handleOpen('edit', scope.row)">
            编辑
          </el-button>
          <el-button type="danger" size="small" @click="delInfo(scope.row.module_id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <Dialog ref="infoDrawer" @doneSave="getTableInfo" />
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, defineEmits, reactive, watch, provide, computed } from "vue";
import Dialog from "./component/dialogForm.vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { changeStatusApi, delApi, getListAPi } from "@/api/carfilm/homeproduct";
const total = ref(0);
const curPage = ref(1);
const pageSize = ref(20);
const tableData = ref([]);
const infoDrawer = ref<any>(null);
const preDrawer = ref<any>(null);
const typeDrawer = ref<any>(null);
const types = ref<any[]>([]);
provide("types", types);
const search = ref({
  categoryName: "",
  name: "",
  categoryIndex: "",
});
onMounted(() => {
  getTableInfo();
});
const resetSearch = () => {
  search.value = {
    categoryName: "",
    name: "",
    categoryIndex: "",
  };
  getTableInfo();
};
const setShow = (row: any) => {
  changeStatusApi(row.id, row.show_in_home).then((res: any) => {
    if (res.code == 1) {
      ElMessage({
        type: "success",
        message: "更新成功！",
      });
    }
    getTableInfo();
  });
};
function getTableInfo() {
  getListAPi({
    ...search.value,
    page: 1,
    limit: 10
  }).then((res: any) => {
    tableData.value = res.data.list.data;
  });
}
function delInfo(id: number) {
  ElMessageBox.confirm("确认要删除该条记录吗?", "警告！", {
    confirmButtonText: "确认删除",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      delApi(id).then((res: any) => {
        getTableInfo();
        ElMessage({
          type: "success",
          message: "删除成功！",
        });
      });
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "删除已取消",
      });
    });
}

// 打开添加数据窗口
const emits = defineEmits(["openForm", "search"]);
function handleOpen(open_type: string, row?: any) {
  infoDrawer.value.open(open_type, row);
}
defineExpose({
  getTableInfo,
});
</script>

<style lang="scss">
.common-seach-wrap {
  display: flex;
  align-items: center;
  justify-content: space-between;

  .el-input {
    width: 300px;
  }
}

.el-table {
  margin-top: 10px;
}

.content-preview {
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}
</style>
